package com.adcc.ags.gms.core.vo;

import com.adcc.ags.gms.core.entity.BaseEntity;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;

import java.util.List;
import java.util.Map;

/**
 * 服务器返回视图层
 */
public class ResultVO<T extends BaseVO> extends BaseVO{

    // 返回状态
    private ResultState state = ResultState.SUCCESS;

    // 返回代码(0为服务器调用正常)
    private int code = 0;

    // 返回原因
    private String reason = Strings.nullToEmpty("");

    //是否进行分页
    private PageInfo<T> pageInfo = new PageInfo<T>();

    // 数据结果集
    private Map<String,List<T>> data = Maps.newHashMap();

    public ResultState getState() {
        return state;
    }

    public void setState(ResultState state) {
        this.state = state;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }

    public Map<String,List<T>> getData(){
        return data;
    }

    public void setData(Map<String,List<T>> data){
        this.data = data;
    }

    public PageInfo<T> getPageInfo() {
        return pageInfo;
    }

    public void setPageInfo(PageInfo<T> pageInfo) {
        this.pageInfo = pageInfo;
    }

    @Override
    public BaseEntity toEntity() {
        return null;
    }

    @Override
    public String toJson() throws JsonProcessingException {
        Map<String,Object> map = Maps.newHashMap();
        map.put("state",state);
        map.put("code",code);
        map.put("reason",Strings.nullToEmpty(reason));
        map.put("pageInfo",pageInfo);
        if(data != null && data.size() > 0){
            map.put("data",data);
        }
        return new ObjectMapper().writeValueAsString(map);
    }
}
